﻿///***************************************
///DATA ACCESS LAYER OF TBLPERSON
///AUTHOR:          QUYET LT
///CREATED DATE:    2013/08/08
///MEMO:    
///***************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using code_sample.Entity;

namespace code_sample.DAL
{
    /// <summary>
    /// DAL class of tblPerson
    /// Author  : QuyetLT
    /// Date    : 2013/08/08
    /// </summary>
    class clsPerson
    {
        /// <summary>
        /// Select single record
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static DataSet fncSelectPerson(Database objDb, int intID)
        {
            try
            {
                SqlParameter[] objParams = new SqlParameter[] { 
                    new SqlParameter("@ID", intID)
                };

                return objDb.ExecuteDataSet("usp_SelectTblPerson", objParams);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Select all record
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static DataSet fncSelectPersonAll(Database objDb)
        {
            try
            {
                return objDb.ExecuteDataSet("usp_SelectTblPersonAll");
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Select record with condition
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static DataSet fncSelectPersonDynamic(Database objDb, string strWhere, string strOrder)
        {
            try
            {
                SqlParameter[] objParams = new SqlParameter[] { 
                    new SqlParameter("@WhereCondition", strWhere)
                    ,new SqlParameter("@OrderByExpression", strOrder)
                };

                return objDb.ExecuteDataSet("usp_SelectTblPersonDynamic", objParams);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Select page of record
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static DataSet fncSelectPersonPaged(Database objDb)
        {
            try
            {
                return objDb.ExecuteDataSet("usp_SelectTblPersonPaged");
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Insert a record
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static int fncInsertPerson(Database objDb, Entity.clsPerson objData)
        {
            try
            {
                SqlParameter[] objParams = new SqlParameter[] { 
                    new SqlParameter("@Name", objData.Name)
                    ,new SqlParameter("@Dob", objData.Dob)
                    ,new SqlParameter("@ID", objData.ID)
                };

                return objDb.ExecuteNonQuery("usp_InsertTblPerson", objParams);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Insert a record or update if existed
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static int fncInsertUpdatePerson(Database objDb, Entity.clsPerson objData)
        {
            try
            {
                SqlParameter[] objParams = new SqlParameter[] { 
                    new SqlParameter("@ID", objData.ID)
                    ,new SqlParameter("@Name", objData.Name)
                    ,new SqlParameter("@Dob", objData.Dob)
                };

                return objDb.ExecuteNonQuery("usp_InsertUpdateTblPerson", objParams);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Update a record
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static int fncUpdatePerson(Database objDb, Entity.clsPerson objData)
        {
            try
            {
                SqlParameter[] objParams = new SqlParameter[] { 
                    new SqlParameter("@ID", objData.ID)
                    ,new SqlParameter("@Name", objData.Name)
                    ,new SqlParameter("@Dob", objData.Dob)
                };

                return objDb.ExecuteNonQuery("usp_UpdateTblPerson", objParams);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Delete a record
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static int fncDeletePerson(Database objDb, int intID)
        {
            try
            {
                SqlParameter[] objParams = new SqlParameter[] { 
                    new SqlParameter("@ID", intID)
                };

                return objDb.ExecuteNonQuery("usp_DeleteTblPerson", objParams);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Delete a record with condition
        /// Author  : QuyetLT
        /// Date    : 2013/08/08
        /// </summary>
        /// <returns>DataSet</returns>
        static int fncDeletePersonDynamic(Database objDb, string strWhere)
        {
            try
            {
                SqlParameter[] objParams = new SqlParameter[] { 
                    new SqlParameter("@WhereCondition", strWhere)
                };

                return objDb.ExecuteNonQuery("usp_DeleteTblPeopleDynamic", objParams);
            }
            catch (Exception ex) { throw ex; }
        }
    }
}
